package com.itheima.condition;

import com.itheima.annotation.ConditionalOnClass;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class OnClassCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        try {
            Map<String, Object> attributes = annotatedTypeMetadata.getAnnotationAttributes(ConditionalOnClass.class.getName());
            String[] name = (String[]) attributes.get("name");
            for (String s : name) {
                Class.forName(s);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
